﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Timers;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using Client.ServerServices;
using System.ServiceModel;

namespace Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public static ServerServices.ServerServicesClient ssc;
        private UserControl loginScreen;
        public UserControl lobbyScreen;
        private UserControl registrationScreen;
        public UserControl gameScreen;
        public ServerServicesClient client;

        public void SetScreen(String type)
        {
            if (type == "login")
            {
                gameTerritory.Children.Clear();
                gameTerritory.Children.Add(loginScreen);
            }
            else if (type == "lobby")
            {
                gameTerritory.Children.Clear();
                lobbyScreen = new Lobby(this);
                gameTerritory.Children.Add(lobbyScreen);
            }
            else if (type == "registration")
            {
                gameTerritory.Children.Clear();
                gameTerritory.Children.Add(registrationScreen);
            }
        }

        public void SetScreenToGame(string tableID, bool isThatTheFirstPlayer, string opponentName, bool cont)
        {
            gameTerritory.Children.Clear();
            gameScreen = new Game(this, tableID, isThatTheFirstPlayer, opponentName,cont);
            gameTerritory.Children.Add(gameScreen);
        }

        public MainWindow()
        {
            InitializeComponent();
            loginScreen = new Login(this);
            registrationScreen = new Registration(this);
            GoToServer();
        }

        public void GoToServer()
        {
            IServerServicesCallback callback = new TwoWayMethods(this);
            InstanceContext context = new InstanceContext(callback);
            client = new ServerServicesClient(context);

            //SetScreenToGame("1", true, "aasd",false);
            SetScreen("login");
            /* DispatcherTimer dispatcherTimer = new DispatcherTimer();

             dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

             dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

             dispatcherTimer.Start();*/

            //ssc = new ServerServices.ServerServicesClient();

            /*IServerServicesCallback callback = new TwoWayMethods();
            InstanceContext context = new InstanceContext(callback);
            using(ServerServicesClient client = new ServerServicesClient(context))
            {
                client. akármi
            }*/

            //Panel.Children.Add(new Game(true,"1"));
            // Panel.Children.Add(new Lobby());
            //gameTerritory.Children.Add(loginScreen);
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {

            /*ServerOnlineClient so = new ServerOnlineClient();
            try
            {
                if (so.DoWork())
                    Status.Content = "Server is online!";
            }
            catch (Exception ex)
            {
                Status.Content = "Server is down!";
            }*/

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Login.username != null)
            {

                    ServerServices.LogoutRequest req = new ServerServices.LogoutRequest() { UserName = Login.username };
                    client.LogoutProcess(req);
                
                /*MainWindow.ssc = new ServerServices.ServerServicesClient();
                ServerServices.LogoutRequest req = new ServerServices.LogoutRequest(){ UserName = Login.username };
                ssc.LogoutProcess(req);*/
            }
            Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
            Application.Current.Shutdown();
        }
    }

    //[CallbackBehavior(UseSynchronizationContext = false)]
    public class TwoWayMethods : IServerServicesCallback
    {
        private MainWindow window;
        public TwoWayMethods(MainWindow r)
        {
            window = r;
        }
        public void StepChangeProcess(StepRequest request, StepServiceResponse response)
        {
            if (response.Kill == false)
            {
                MessageBox.Show(request.UserID + " innen - " + request.StepFrom.X + request.StepFrom.Y + " ide - " + request.StepTo.X + request.StepTo.Y + " lépett! Te következel!");
                Game gameUC = (Game)window.gameScreen;
                gameUC.PlacePiece(request);
                gameUC.TurnChanger();
            }
            else
            {
                MessageBox.Show(request.UserID + " innen - " + request.StepFrom.X + request.StepFrom.Y + " ide - " + request.StepTo.X + request.StepTo.Y + " lépett és leütötte egy bábud!Te következel!");
                Game gameUC = (Game)window.gameScreen;
                gameUC.TurnChanger();
                gameUC.PlacePiece(request);
                gameUC.DisappearPiece(request, response);
            }
        }

        public void GameResultChangeProcess(string Username, int type)
        {
            //ELO pont számítás Game cs-ben
            Game gameUC = (Game)window.gameScreen;
            switch (type)
            {
                case 1:
                    MessageBox.Show("Játék vége! A nyertes: " + Username);
                    gameUC.EndOfGame();
                    break;
                case 2:
                    MessageBox.Show("Ellenfél feladta! A nyertes: " + Username);
                    gameUC.EndOfGame();
                    break;
            }
        }

        public void StartGameChangeProcess(string Username)
        {
            MessageBox.Show("Ellenfél belépett: " + Username + " Játék kezdődik!");
            Game gameUC = (Game)window.gameScreen;
            gameUC.OpponentArrived(Username);
        }

        public void ListGameChangeProcess()
        {
            MessageBox.Show("Játéklista változott. Frissítsd a listát!");
            Lobby lobbyUC = (Lobby)window.lobbyScreen;
            lobbyUC.RefreshGame.BorderBrush = new SolidColorBrush(Colors.Red);
            lobbyUC.RefreshGame.BorderThickness = new Thickness(15.0);
        }
        public void GetMsgChangeProcess(string Username)
        {
            MessageBox.Show("Új chat üzenet elérhető. Frissíts!");
            Game gameUC = (Game)window.gameScreen;
            gameUC.RefreshChatBox.BorderBrush = new SolidColorBrush(Colors.Red);
            gameUC.RefreshChatBox.BorderThickness = new Thickness(15.0);
        }
    }
}
